<?php

class _F_EXTENSION_FORM_ extends _F_EXTENSION_ {
	
	public static $widgets = array(); // TODO: doksiba... ez mire valo?

	public $validators;
	
	public function __construct() {
		$dir = dirname(__FILE__);
		require_once $dir.'/form.object.php';
		$this->validators = array(
			'string' => array($this,'string_validator'),
			'email' => array($this,'email_validator'),
			'number' => array($this,'number_validator'),
			'date' => array($this,'date_validator'),
			'time' => array($this,'time_validator'),
		);		
		f()->template_path_list[] = dirname(__FILE__).'/templates';
		f()->register_method('form',array($this,'form'));
	}
	
	public function form($id = null,$title = null,$options = null) {
		f()->style('/extensions/form/styles/form.less');
		f()->import('jquery');
		f()->jquery();
		f()->script('/extensions/form/scripts/form.js');
		if ($id === null) return $this;
		$form = new _F_EXTENSION_FORM_OBJECT_($id,$title,$options);
		$this->put_registered_widgets($form);
		return $form;
	}
	
	public function register_widget($name,$fn) {
		_F_EXTENSION_FORM_::$widgets[$name] = $fn;
		return $this;
	}

	public function put_registered_widgets($form) {
		foreach (_F_EXTENSION_FORM_::$widgets as $name => $fn) 
			$form->register_method($name,$fn);
	}
	
	// validators
		
	/*
		name: length
		description: ensure the minimum and/or the maximum length of the field
		params: 
			length - the exact length
			min - the minimum length
			max - the maximum length
			allow_empty - allows an empty string (true)			
			pattern - the required pattern
			length_message - the response message if the field is not the exact size
			min_message - the response message if the field is too short
			max_message - the response message if the field is too long
			allow_empty_message - the response message if the field is empty
			pattern_message - the response message if the field doesn't match the pattern
	*/
	public function string_validator($form,&$element,$params,&$messages) {
		$value = isset($element['value']) ? $element['value'] : '';
		$length = strlen($value);
		if (isset($params['length']) && $length != $params['length']) {
			$messages[$element['id']] = isset($params['length_message']) ?
				$params['length_message'] :
				t("The field has to be {$params['length']} characters.");
		} else if (array_key_exists('allow_empty',$params) && 
				!$params['allow_empty'] && $length == 0) {
			$messages[$element['id']] = isset($params['allow_empty_message']) ?
				$params['allow_empty_message'] :
				t("The field cannot be empty.");
		} else if (isset($params['min']) && $length < $params['min']) {
			$messages[$element['id']] = isset($params['min_message']) ?
				$params['min_message'] :
				t("The field cannot be shorter then {$params['min']} character.");
		} else if (isset($params['max']) && $length > $params['max']) {
			$messages[$element['id']] = isset($params['max_message']) ?
				$params['max_message'] :
				t("The field cannot be longer then {$params['max']} character.");
		} else if (isset($params['pattern']) && preg_match($params['pattern'],$value) != 1) {
			$messages[$element['id']] = isset($params['pattern_message']) ?
				$params['pattern_message'] :
				t("Wrong format");
		} else {
			return true;
		}
	}

	/*
		name: email
		description: check if the filed contains a valid email address
		params: 
			message - the response message
	*/
	public function email_validator($form,&$element,$params,&$messages) {
		$params2 = array('pattern' => '/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i',
			'pattern_message' => isset($params['message']) ? $params['message'] :
			t('Invalid email address.'));
		return $this->validator_string($form,$element,$params2,$messages);
	}
	
	/*
		name: number
		description: check if the filed contains a valid number
		params:
		 	integer_only - accepts integer numbers only (true)
			message - the response message
	*/
	public function number_validator($form,&$element,$params,&$messages) {
		if (array_key_exists('integer_only',$params) && !$params['integer_only']) {
			$pattern = '/^-?\d+(\.\d+)?$/i';
			$message = t('Invalid number.');
		} else {
			$pattern = '/^-?\d+$/i';
			$message = t('Invalid integer.');
		}
		$params2 = array('pattern' => $pattern,
			'pattern_message' => isset($params['message']) ? $params['message'] : $message);
		return $this->validator_string($form,$element,$params2,$messages);
	}

	/*
		name: date
		description: check if the filed contains a valid date(time)
		params:
			message - the response message
	*/
	public function date_validator($form,&$element,$params,&$messages) {
		if (isset($element['value'])) {
			$date = date_parse($element['value']);
			if ($date['warning_count'] + $date['error_count'] == 0 &&
				checkdate($date['month'],$date['day'],$date['year'])) return true;
		}
		$messages[$element['id']] = isset($params['message']) ? 
			$params['message'] : t('Invalid date.');
		return false;
	}

	/*
		name: time
		description: check if the filed contains a valid time
		params:
			message - the response message
	*/
	public function time_validator($form,&$element,$params,&$messages) {
		if (isset($element['value'])) {
			$date = $element['value'];
			if (strlen(trim($date)) > 0) {
				$date = date_parse('1/1/1 '.$element['value']);
				if ($date['warning_count'] + $date['error_count'] == 0 &&
					checkdate($date['month'],$date['day'],$date['year'])) return true;
			}
		}
		$messages[$element['id']] = isset($params['message']) ? 
			$params['message'] : t('Invalid time.');
		return false;
	}	
}

new _F_EXTENSION_FORM_;
